package com.zgs.mall.seckill.interceptor;

import com.zgs.common.constant.LoginConstant;
import com.zgs.mall.seckill.vo.MemberRespVo;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登录拦截器
 */
@Component
public class SeckillUserLoginInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberRespVo> userInfo = new ThreadLocal<MemberRespVo>();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        //判断是否登录
       HttpSession session = request.getSession();
        MemberRespVo memberRespVo=  (MemberRespVo)session.getAttribute(LoginConstant.USER_LOGIN);
        if(memberRespVo !=null){//登录了
            userInfo.set(memberRespVo);
            return true;
        }else{
            //没登录
            request.getSession().setAttribute("msg","请先登录");
            response.sendRedirect("http://auth.mall.com/login.html");
            return false;
        }

    }
}
